package mrsimulator;

import java.util.LinkedList;
import java.util.List;

/**
 * DumbScheduler is only for testing
 */
public class DumbScheduler implements Scheduler{

    private JobTrackerSimulator motherBoard;

    public DumbScheduler(JobTrackerSimulator mobo) {
        this.motherBoard = mobo;
    }

    /**
     * Dumb scheduler always return the first job in the queue
     * Just for testing
     * *NOTE* -- Lock jipqm before calling this function
     * */

    @Override
    public List<JobTrackerSimulator.TaskInProgress> assignTask(TaskTrackerStatus status) {
        int mapSlot = status.getMapSlot();
        int reduceSlot = status.getReduceSlot();
        List<JobTrackerSimulator.JobInProgress> jobQueue = motherBoard.getJobQueue();
        List<JobTrackerSimulator.TaskInProgress> ret = new LinkedList<JobTrackerSimulator.TaskInProgress>();
        if(jobQueue.size() == 0){
            // If there is no job in the queue
            return ret;
        }

        // always pick the first job from queue, because it's dumb scheduler and only one job is submitted
        JobTrackerSimulator.JobInProgress headOfQueue = jobQueue.get(0); // this won't return null, since jobQ.size is not 0

        // Launch map tasks
        while (mapSlot > 0){
            JobTrackerSimulator.TaskInProgress tip = headOfQueue.pickMapTaskToRun(status);
            if(tip != null){
                ret.add(tip);
                tip.markTaskAsLaunched();
                mapSlot--;
            } else {
                break; // the head of queue has no task to schedule, just skip
            }
        }

        // Launch reduce tasks
        while (reduceSlot > 0){
            JobTrackerSimulator.TaskInProgress tip = headOfQueue.pickReduceTaskToRun(status);
            if(tip != null){
                ret.add(tip);
                tip.markTaskAsLaunched();
                reduceSlot--;
            } else {
                break; // the head of queue has no task to schedule, just skip
            }
        }

        return ret;

        /*
        int scannedItem = 0;
        List<JobTrackerSimulator.TaskInProgress> resultOfScan = new LinkedList<JobTrackerSimulator.TaskInProgress>();

        // Schedule map task
        while (mapSlot > 0){
            JobTrackerSimulator.JobInProgress headOfQueue = jobQueue.get(0); // this won't return null, since jobQ.size is not 0
            JobTrackerSimulator.TaskInProgress tip = headOfQueue.pickMapTaskToRun(status);
            if(tip != null){
                tip.markTaskAsLaunched();
                resultOfScan.add(tip);
                mapSlot--;
            }
            motherBoard.roundRobin();
            scannedItem++;
            if(scannedItem == jobQueue.size()){
                if(resultOfScan.size() != 0){
                    // finished one scan and get some result
                    ret.addAll(resultOfScan);
                    resultOfScan = new LinkedList<JobTrackerSimulator.TaskInProgress>();
                    scannedItem = 0;
                } else {
                    // finished one scan but get nothing
                    break;
                }
            }
        }


        // Schedule reduce task
        int reduceSlot = status.getReduceSlot();
        while (reduceSlot > 0){

        }
        */
    }
}
